﻿#pragma once
#include "../../src_basics/dataStructAlg.h"

class Via {
public:
	Via() : m_radius(0) {};
	Via(double radius, std::vector<int>& layers)
		: m_radius(radius), m_layers(layers) {
	}
	~Via() {};

public:
	double m_radius;
	std::vector<int> m_layers;
};

class PinPolygon {
public:
	PinPolygon(Point& center, std::vector<int>& layers, std::vector<Line>& edges, double radius = -1)
		: m_center(center), m_layers(layers), m_edges(edges), m_radius(radius) {
		PinPolygonInit();
	};
	PinPolygon(const PinPolygon& pinPolygon)
		: m_center(pinPolygon.m_center),
		m_box(pinPolygon.m_box),
		m_layers(pinPolygon.m_layers),
		m_edges(pinPolygon.m_edges),
		m_radius(pinPolygon.m_radius) {
	};

	~PinPolygon() {};

public:
	Point m_center;
	std::vector<double> m_box = { 0,0,0,0 };  // minx,miny,maxx,maxy
	std::vector<Line> m_edges;
	std::vector<int> m_layers;
	double m_radius = -1;

private:
	void PinPolygonInit() {
		if (m_radius <= 0) {	//多边形，根据边计算最小包围盒
			if (m_edges.empty())
				return;
			m_box = { m_edges[0].Pt1.x,m_edges[0].Pt1.y,m_edges[0].Pt1.x,m_edges[0].Pt1.y };
			for (auto& edge : m_edges) {
				m_box[0] = std::min(m_box[0], std::min(edge.Pt1.x, edge.Pt2.x));
				m_box[1] = std::min(m_box[1], std::min(edge.Pt1.y, edge.Pt2.y));
				m_box[2] = std::max(m_box[2], std::max(edge.Pt1.x, edge.Pt2.x));
				m_box[3] = std::max(m_box[3], std::max(edge.Pt1.y, edge.Pt2.y));
			}
			// 特殊处理，用矩形外包盒代替多边形
			m_edges.clear();
            m_edges.emplace_back(Line(Point(m_box[0], m_box[1]), Point(m_box[2], m_box[1])));
			m_edges.emplace_back(Line(Point(m_box[2], m_box[1]), Point(m_box[2], m_box[3])));
			m_edges.emplace_back(Line(Point(m_box[2], m_box[3]), Point(m_box[0], m_box[3])));
			m_edges.emplace_back(Line(Point(m_box[0], m_box[3]), Point(m_box[0], m_box[1])));
		}
		else {	// 圆，将m_edges设置为外接正八边形，计算八边形的最小包围盒
			m_edges.clear();
			double d = m_radius * tan(PI / 8);
			Point p1(m_center.x + m_radius, m_center.y + d);
			Point p2(m_center.x + m_radius, m_center.y - d);
			Point p3(m_center.x + d, m_center.y - m_radius);
			Point p4(m_center.x - d, m_center.y - m_radius);
			Point p5(m_center.x - m_radius, m_center.y - d);
			Point p6(m_center.x - m_radius, m_center.y + d);
			Point p7(m_center.x - d, m_center.y + m_radius);
			Point p8(m_center.x + d, m_center.y + m_radius);
			m_edges.emplace_back(Line(p1, p2));
			m_edges.emplace_back(Line(p2, p3));
			m_edges.emplace_back(Line(p3, p4));
			m_edges.emplace_back(Line(p4, p5));
			m_edges.emplace_back(Line(p5, p6));
			m_edges.emplace_back(Line(p6, p7));
			m_edges.emplace_back(Line(p7, p8));
			m_edges.emplace_back(Line(p8, p1));

			m_box = { m_center.x - m_radius,m_center.y - m_radius,m_center.x + m_radius,m_center.y + m_radius };
		}
	}
};